require 'test/unit'

require 'models/room'
require 'models/portal'

class PortalTest < Test::Unit::TestCase
    def setup
        if @door.nil? or @door.oid.nil? then
            @door = Portal.new
            @door.name = 'Door'
            @door = PortalDatabase.instance.add(@door)
        end
        if @portal.nil? or @portal.oid.nil? then
            @portal = Portal.new
            @portal.name = 'Portal'
            @portal = PortalDatabase.instance.add(@portal)
        end
        if @start.nil? or @start.oid.nil? then
            @start = Room.new
            @start.name = 'Start'
            @start.add_portal(@door)
            @start.add_portal(@portal)
            @start = RoomDatabase.instance.add(@start)
        end

        if @finish.nil? or @finish.oid.nil? then
            @finish = Room.new
            @finish.name = 'Finish'
            @finish.add_portal(@door)
            @finish.add_portal(@portal)
            @finish = RoomDatabase.instance.add(@finish)
        end
    end
    def test_room_without_exits
        r = Room.new
        assert_equal([],r.exits)
    end

    def test_rooms_with_portals
        assert_equal(2,@start.portals.size)
        assert_equal(2,@finish.portals.size)
        # assert_equal(2,RoomDatabase.instance.size)
    end
    def test_room_with_exits
        @door.leads("North", :from => @start, :to => @finish)
        assert_equal(1,@door.entries.size)
        assert_equal(1,@start.exits.size)
        assert_equal(0,@finish.exits.size)

        @door.goes("South", :from => @finish, :to => @start)
        assert_equal(2,@door.entries.size)
        assert_equal(1,@start.exits.size)
        assert_equal(1,@finish.exits.size)
    end
    def test_room_with_looped_exit
        @portal.loop(@start,"In",@finish,"Out")
        assert_equal(2,@portal.entries.size)
        assert_equal(1,@start.exits.size)
        assert_equal(1,@finish.exits.size)
    end
    def test_room_both
        @door.leads("North", :from => @start, :to => @finish)
        assert_equal(1,@door.entries.size)
        assert_equal(1,@start.exits.size)
        assert_equal(0,@finish.exits.size)

        @door.goes("South", :from => @finish, :to => @start)
        assert_equal(2,@door.entries.size)
        assert_equal(1,@start.exits.size)
        assert_equal(1,@finish.exits.size)

        @portal.loop(@start,"In",@finish,"Out")
        assert_equal(2,@portal.entries.size)
        assert_equal(2,@start.exits.size) # portal,door
        assert_equal(2,@finish.exits.size) # portal, door
    end
end
